package cn.jekun.web.shop.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import cn.jekun.component.base.bean.Page;
import cn.jekun.component.base.utils.SqlUtis;
import cn.jekun.component.base.validator.annotation.D;
import cn.jekun.component.base.validator.annotation.NotBlank;
import cn.jekun.component.base.validator.annotation.Num;
import cn.jekun.component.base.validator.annotation.Security;
import cn.jekun.component.core.bean.gen.ErpShop;
import cn.jekun.component.core.bean.gen.TsAccount;
import cn.jekun.component.core.bean.gen.TsAccountJournal;
import cn.jekun.component.core.bean.gen.TsOperationCode;
import cn.jekun.web.shop.bean.ShopAccounts;

/**
 * 账户流水查询Service
 * @author yuan
 *
 */
@Service(AccountService.BEAN_NAME)
public class AccountService extends ShopBaseService{
	public static final String BEAN_NAME="cn.jekun.web.shop.service.AccountService";
	@Override
	public String getNamespace() {
		return "SHOP_ADMIN_ACTION_TS_ACCOUNT_DETAILS";
	}

//	/**
//	 * 获取余额
//	 * @param ShopId
//	 * @return
//	 */
//	public double[] getBalance(@ErpShop.AnnoShopId @NotBlank(message="商家编码不能为空") String ShopId){
//		try {
//			return accountDao.getShopBalance(ShopId);
//		} catch (SQLException e) {
//			e.printStackTrace();
//		}
//		return null;
//	}

	/**
	 * 获取账户流水明细
	 * @param map
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public List<ShopAccounts> getAccountDetails(@Num(length = 1) String queryDate,
												@TsAccountJournal.AnnoOperationCode String operType,
												@Num(length = 10,scale=2,message="请输入正确的金额") String beginMoney,
												@Num(length = 10,scale=2,message="请输入正确的金额") String endMoney,
												@Security String keyRange,
												@D String beginDate,
												@D String endDate,
												@Security String keyword_s,
												@ErpShop.AnnoShopId String shopId,
												Page page,
												@Security String orderId){
		
		String SQL ="";
		if(queryDate!=null && queryDate.toString().length()>0){
			if("1".equals(queryDate)){ 
				SQL +=" and trunc(a.CREATE_TIME) = trunc(sysdate)";  //今天的数据
			}else if("2".equals(queryDate)){
				SQL +=" and trunc(a.create_time) >= trunc(add_months(sysdate,-1))"; //最近一个月的数据
			}else if(queryDate!=null && "3".equals(queryDate)){
				SQL +=" and trunc(a.create_time) >= trunc(add_months(sysdate,-3))";  //3个月的数据
			}else if("4".equals(queryDate)){
				SQL +=" and trunc(a.create_time) >= trunc(add_months(sysdate,-12))"; //1年的数据
			}else if("5".equals(queryDate)){ //1年前的数据
				SQL +=" and trunc(a.create_time) <= trunc(add_months(sysdate,-12))";	
			}
		}if(operType!=null && operType.toString().length()>0){
			SQL +=" and a.OPERATION_CODE = '"+operType.toString()+"'"; 
		}if(!"".equals(beginMoney) && "".equals(endMoney)){
			SQL +=" and a.amount>="+beginMoney; 
		}if("".equals(beginMoney) && !"".equals(endMoney)){
			SQL +=" and a.amount<="+endMoney; 
		}if(beginMoney!=null && endMoney!=null && !"".equals(beginMoney) 
				&& !"".equals(endMoney)){
			SQL +=" and a.amount>="+beginMoney+" and a.amount<="+endMoney+""; 
		}
		if("".equals(beginDate) && !"".equals(endDate)){
			SQL +=" and a.create_time"
					+ "<= to_date('"+endDate.toString().trim()+" 23:59:59','yyyy/MM/dd hh24:mi:ss')";
		}if(!"".equals(beginDate) && "".equals(endDate)){
			SQL +=" and a.create_time"
					+ ">= to_date('"+beginDate.toString().trim()+"','yyyy/MM/dd hh24:mi:ss')";
		}if(!"".equals(beginDate) && beginDate!=null 
				&& !"".equals(endDate) && endDate!=null){
			SQL +=" and a.create_time >= to_date('"+beginDate.toString().trim()+"','yyyy/MM/dd hh24:mi:ss')"
					+ " and a.create_time <= to_date('"+endDate.toString().trim()+" 23:59:59','yyyy/MM/dd hh24:mi:ss')"; 
		}
		if(keyRange!=null && keyword_s.toString().length()>0 ){
			if("bill".equals(keyRange)){
				SQL +=" and a.account_journal_id like '%"+keyword_s.toString()+"%'"; 	
			}else if("name".equals(keyRange)){
				SQL +=" and e.operation like '%"+keyword_s.toString()+"%'"; 
			}

		}if(orderId!=null && orderId.toString().length()>0){
			SQL +=" and a.order_id like '%"+orderId.toString()+"%'"; 
		}
		System.out.println("SQL="+SQL);
		Map formMap=new HashMap<String, String>();
		formMap.put("shopId", shopId);
		SqlUtis.prepareSql(formMap,SQL);
		return this.list(this.getStatementName("list"),formMap,page);


	}
	
	
	/**
	 * 获取账户流水明细
	 * @param map
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public TsAccount one(@ErpShop.AnnoShopId String shopId){
		Map map=new HashMap<String, String>();
		map.put("shopId", shopId);
		return this.one(this.getStatementName("query"),map);


	}

	/**
	 * 获取账户流水类型
	 * @return
	 */
	public List<TsOperationCode> getOperTypes(){

		return this.list(this.getStatementName("queryoperationcode"),null);
	}

	/**
	 * 获取日期条件
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public List<ShopAccounts> getQueryDate(@Num(length = 1)String queryDate){
		String SQL ="";
		Map map=new HashMap<String, String>();
		if(queryDate!=null && queryDate.toString().length()>0){
			if("1".equals(queryDate)){ 
				SQL +=" to_char(sysdate,'yyyy-MM-dd') beginDate,to_char(sysdate,'yyyy-MM-dd') endDate";  //今天
			}else if("2".equals(queryDate)){
				SQL +="to_char(add_months(sysdate,-1),'yyyy-MM-dd') beginDate, to_char(sysdate,'yyyy-MM-dd') endDate"; //最近一个月
			}else if(queryDate!=null && "3".equals(queryDate)){
				SQL +="to_char(add_months(sysdate,-3),'yyyy-MM-dd') beginDate, to_char(sysdate,'yyyy-MM-dd') endDate";  //3个月
			}else if("4".equals(queryDate)){
				SQL +="to_char(add_months(sysdate,-12),'yyyy-MM-dd') beginDate,to_char(sysdate,'yyyy-MM-dd') endDate"; //1年
			}else if("5".equals(queryDate)){ //1年前
				SQL +="to_char(add_months(sysdate,-12),'yyyy-MM-dd') endDate";	
			}
			map.put("COLUMN_SQL", SQL);
			return this.list(this.getStatementName("getQueryDate"),map);
		}else{
			SQL += "to_char(min(create_time),'yyyy-MM-dd') beginDate,to_char(max(create_time),'yyyy-MM-dd') endDate";
			map.put("COLUMN_SQL", SQL);
			return this.list(this.getStatementName("getQueryDate2"),map);
		}

	}
	
	/**
	 * 获取账户提现详情
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public ShopAccounts  getAccountDetailsById( @NotBlank(message="流水编码不能为空") @TsAccountJournal.AnnoAccountJournalId String accountJourId){
		Map map=new HashMap<String, String>();
		map.put("accountJourId", accountJourId);
		return this.one(this.getStatementName("getAccountsDetailsById"),map);
	}
	
}
